package com.topboya.linker.ui.adapter;

import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.topboya.linker.AppConst;
import com.topboya.linker.R;
import com.topboya.linker.model.response.HomeListResponse;
import com.topboya.linker.ui.activity.xMainActivity;
import com.topboya.linker.ui.presenter.HomePresenter;
import com.topboya.linker.utils.LogUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class HomeList1Adapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private static final String TAG = "HomeList1Adapter:";
    private xMainActivity mainActivity;
    private List<HomeListResponse.TaskList> taskLists;
    private HomePresenter homePresenter;
    private final int EMPTY_VIEW = 0;
    private final int NOT_EMPTY_VIEW = 1;

    public HomeList1Adapter(xMainActivity mainActivity, HomePresenter homePresenter, List<HomeListResponse.TaskList> taskLists) {

        this.mainActivity = mainActivity;
        this.homePresenter = homePresenter;
        this.taskLists = taskLists;

    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {

        if (viewType ==EMPTY_VIEW){

            View itemView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.task_empty_item, viewGroup, false);
            return new HomeList1Adapter.EmptyViewHolder(itemView);
        }else {

            View itemView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.home_task_item, viewGroup, false);
            return new HomeList1Adapter.ListItemViewHolder(itemView);
        }

    }

    //绑定界面，设置监听
    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int i) {
        ListItemViewHolder h;
        if(holder instanceof EmptyViewHolder){
            LogUtils.v(TAG+"EMPPTY");
            return;
        }else {

            h = (ListItemViewHolder) holder;
        }
        h.tv_orgname.setText(taskLists.get(i).getOrgName());
        h.tv_cusname.setText(taskLists.get(i).getCusName());
        h.tv_timekey.setText(taskLists.get(i).getTimeKey());
        if (null != taskLists.get(i).getStatus()) {

            int status = Integer.parseInt(taskLists.get(i).getStatus());

            String[] statusAaary = mainActivity.getResources().getStringArray(R.array.signin_status);
            if (status==1||status ==2||status ==4){
             LogUtils.v(TAG +"!AppConst.isMyTeam:"+!AppConst.isMyTeam);
                if (!AppConst.isMyTeam){

                    h.tv_status.setVisibility(View.VISIBLE);
                    h.tv_status.setText(statusAaary[status]);
                    h.tv_status.setBackgroundResource(getSignStatusBg(status));
                    h.tv_status.setTextColor(mainActivity.getResources().getColor(R.color.task_complete_coloe));
//             暂时三种状态显示1 签入 2 反馈（已签入可签出） 4 正常签出 其他状态不显示
                    if (status==4){

                        h.tv_endtiem.setVisibility(View.VISIBLE);
                        h.tv_endtiem.setText(getTime(taskLists.get(i).getEndTime()));
                        h.tv_endtiem.setBackgroundColor(0XFFD9E0EB);
                        h.tv_endtiem.setTextColor(0xFFFFFFFF);
//                表示正常签出，就显示签出时间，否则就不显示

                    }else {
                        h.tv_endtiem.setVisibility(View.GONE);
                        h.tv_status.setTextColor(0xFFFFFFFF);
                    }
                }else {

                    if (status ==1){

                        h.tv_status.setVisibility(View.VISIBLE);
                        h.tv_status.setText("签");
                        h.tv_status.setBackgroundColor(Color.WHITE);
                        h.tv_status.setTextColor(Color.BLUE);
                        h.tv_endtiem.setVisibility(View.GONE);

                    } else if (status==4){

                        h.tv_status.setVisibility(View.VISIBLE);
                        h.tv_status.setText(statusAaary[status]);
                        h.tv_status.setBackgroundResource(getSignStatusBg(status));
                        h.tv_status.setTextColor(mainActivity.getResources().getColor(R.color.task_complete_coloe));
//             暂时三种状态显示1 签入 2 反馈（已签入可签出） 4 正常签出 其他状态不显示
                        h.tv_endtiem.setVisibility(View.VISIBLE);
                        h.tv_endtiem.setText(getTime(taskLists.get(i).getEndTime()));
                        h.tv_endtiem.setBackgroundColor(0XFFD9E0EB);
                        h.tv_endtiem.setTextColor(0xFFFFFFFF);
//                表示正常签出，就显示签出时间，否则就不显示

                    }else {

                        h.tv_endtiem.setVisibility(View.GONE);
                        h.tv_endtiem.setVisibility(View.GONE);
                    }

                }

            }else {

                h.tv_status.setVisibility(View.GONE);
                h.tv_endtiem.setVisibility(View.GONE);
            }
        }else {

            h.tv_status.setVisibility(View.GONE);
            h.tv_endtiem.setVisibility(View.GONE);
        }

        if (null !=itemLongClickListener){

            holder.itemView.setOnLongClickListener(v -> {

                itemLongClickListener.onLongClick(holder.itemView,i);
                return false;

            });
        }

        if (null !=itemClickListener){

              holder.itemView.setOnClickListener(v -> itemClickListener.onClick(h.itemView,i));
        }

        if (null !=itemChildClickListener){

            h.tv_status.setOnClickListener(v -> itemChildClickListener.onclick( h.tv_status,i));
        }

    }

    @Override
    public int getItemCount() {
        if (null ==taskLists ||taskLists.size()==0){

            return 1;
        }else {

            return taskLists == null ? 0 : taskLists.size();
        }

    }


    @Override
    public int getItemViewType(int position) {

        if (null !=taskLists && taskLists.size()==0){

            return EMPTY_VIEW;
        }else {

            return NOT_EMPTY_VIEW;
        }

    }
    class EmptyViewHolder extends RecyclerView.ViewHolder{

        public EmptyViewHolder(View itemView) {
            super(itemView);
        }

    }

    public class ListItemViewHolder extends RecyclerView.ViewHolder{

        //ViewHolder
        TextView tv_timekey;
        TextView tv_orgname;
        TextView tv_cusname;
        TextView tv_endtiem;
        TextView tv_status;
        LinearLayout ll_sign_status;

        ListItemViewHolder(View view) {

            super(view);
            this.tv_timekey = view.findViewById(R.id.tv_timekey);
            this.tv_orgname = view.findViewById(R.id.tv_orgname);
            this.tv_cusname = view.findViewById(R.id.tv_cusname);
            this.tv_endtiem = view.findViewById(R.id.tv_endtiem);
            this.tv_status = view.findViewById(R.id.tv_status);
            this.ll_sign_status = view.findViewById(R.id.ll_sign_status);

        }

    }

    private String getTime(String time) {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        try {
            Date date = sdf.parse(time);
            SimpleDateFormat format = new SimpleDateFormat("HH:mm");

            String data = format.format(date);
            return data ;
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return "";
    }


//0:不能签入
//1:可签入
//2:已签入可迁出
//3:已签入超过三公里
//4：正常签出
//5：非正常签出
//6：非正常签出
    public int getSignStatusBg(int status){
        int  bgid;
        switch (status){
            case 1:
                bgid =  R.drawable.signin_oval_bg;
                break;
            case 2:
                bgid =  R.drawable.feedback_oval_bg;
                break;
            case 4:
                bgid =  R.drawable.complete_oval_bg;
                break;
            default:
                bgid =  R.drawable.complete_oval_bg;
                break;
        }
        return bgid;
    }
    private OnItemClickListener itemClickListener;

    public void setOnItemClickListener(OnItemClickListener itemClickListener){

        this.itemClickListener = itemClickListener;
    }

    public interface  OnItemClickListener{

        void onClick(View v,int position);
    }

    private OnItemLongClickListener itemLongClickListener;

    public void setOnItemLongClickListener(OnItemLongClickListener itemLongClickListener){

        this.itemLongClickListener = itemLongClickListener;
    }
    public interface  OnItemLongClickListener{

        void onLongClick(View v,int position);
    }

    private OnItemChildClickListener itemChildClickListener;

    public void setOnitemChildCkickListener(OnItemChildClickListener itemChildClickListener){

        this.itemChildClickListener = itemChildClickListener;
    }
    public interface  OnItemChildClickListener{

        void onclick(View view,int position);
    }
}
